package ar.com.quickmart.classes;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.logging.Logger;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import ar.com.quickmart.R;

public class HistoryListAdapter extends BaseAdapter {

	private final Context mContext;
	private final List<HistoryList> mPurchases;
	
	public HistoryListAdapter(Context context, List<HistoryList> purchases) {
		super();
		mContext = context;
		mPurchases = purchases;
	}
	
	@Override
	public int getCount() {
		return mPurchases.size();
	}

	@Override
	public Object getItem(int position) {
		return mPurchases.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		// La vista que contiene el layout de la fila voy a devolver
		View row = convertView;
		
		PurchaseHolder holder = new PurchaseHolder();
			
		if(row == null) {
			
			LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			row = inflater.inflate(R.layout.list_item_purchase, parent, false);
			
			holder.txtName = (TextView) row.findViewById(R.id.listName);
			holder.txtPrice = (TextView) row.findViewById(R.id.purchasePrice);
			holder.txtState = (TextView) row.findViewById(R.id.purchaseState);
			holder.txtDate = (TextView) row.findViewById(R.id.purchaseDate);
			
			row.setTag(holder);			
		}
		else {
					holder = (PurchaseHolder) row.getTag();
		}		

			holder.txtName.setText(mPurchases.get(position).getListName());
			holder.txtState.setText(mPurchases.get(position).getState() + " ");
			holder.txtState.setTextColor(PurchaseState.getColorIdByString((mPurchases).get(position).getState()));
			holder.txtPrice.setText("$" + String.format("%.2f", mPurchases.get(position).getTotalToPay()) + " " );
			SimpleDateFormat date = new SimpleDateFormat("EEE dd/MM/yyyy HH:mm");
			holder.txtDate.setText(date.format(mPurchases.get(position).getConfirmationDate()).toString() + " hs.");
			
		return row;
	}
	
	private static class PurchaseHolder {
		public TextView txtName;
		public TextView txtPrice;
		public TextView txtDate;
		public TextView txtState;
	}

}
